﻿using System;

using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.EditorExt;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.Data;
using DIWAGIS.Modules;
using Microsoft.Office;
using DIWAGIS.Class;
using System.Reflection;
using System.Drawing;


namespace DIWAGIS.ApplicationLayer.ThongKeTaiSan
{
    public partial class frmThongKeDuongOngNhanh : Form
    {
        string sTableName = generalVariable.database + "." + generalVariable.schema + "." + ClassVariables.wOngNhanh;
        Boolean isLoadComBox = false;
        Boolean kt = false;
        long sNResuilt = 0;
        public frmThongKeDuongOngNhanh()
        
        {
            InitializeComponent();
        }

        private void frmThongKeDuongOngNhanh_Load(object sender, EventArgs e)
        {
            progressBarTienTrinh.Visible = false;
            DIWAGIS.Module.WorkingWithDomain cbx = new DIWAGIS.Module.WorkingWithDomain();
            cbx.loadDataDomainToCombobox(comboBoxNuocSanXuat, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMNuocSanXuatOngNuoc");
            cbx.loadDataDomainToCombobox(comboBoxVatLieuOngNhanh, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "VL_ONHANH");
            cbx.LoadSubTypeToCombobox(comboBoxCoOng, sTableName, "CoOng", DIWAGIS.Modules.generalVariable.ICurrentWorkspace);
            SqlCommand sqlcommand = new SqlCommand();

            sqlcommand.CommandText = "SELECT TENHUYEN,TENHUYEN FROM HUYEN ";
            cbx.loadDataToCombobox(comboBoxQuanHuyen, sqlcommand);

            sqlcommand.CommandText = "SELECT TENCONDUONG,TENCONDUONG FROM " + generalVariable.database + "." + generalVariable.schema + "." + " CONDUONG ";
            cbx.loadDataToCombobox(comboBoxTuyenDuong, sqlcommand);

            isLoadComBox = true;
        }

        private void comboBoxQuanHuyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isLoadComBox == true)
            {
                string strWhere = "";
                if (!string.IsNullOrEmpty(comboBoxQuanHuyen.Text))
                {
                    strWhere = comboBoxQuanHuyen.SelectedValue.ToString();
                    SqlCommand sqlcommand = new SqlCommand();
                    DIWAGIS.Module.WorkingWithDomain cbx = new DIWAGIS.Module.WorkingWithDomain();
                    //sqlcommand = new System.Data.SqlClient.SqlCommand();
                    sqlcommand.CommandText = "SELECT TenHanhChinh,TenHanhChinh FROM " + generalVariable.database + "." + generalVariable.schema + "." + "HANHCHINH" + " WHERE TenHuyen = @TenHuyen";
                    sqlcommand.Parameters.AddWithValue("@TenHuyen", strWhere);
                    cbx.loadDataToCombobox(comboBoxPhuongXa, sqlcommand);

                    //loadDataToCombobox(cbxMaPhuong, sqlcommand);

                }
            }
        }

        private void buttonTimKiem_Click(object sender, EventArgs e)
        {
            IFeatureWorkspace pFWS = DIWAGIS.Modules.generalVariable.ICurrentWorkspace as IFeatureWorkspace;

            string strWhere = "";

            if (txtMaDMA.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND MaDMA ='" + (txtMaDMA.Text) + "'";
                }
                else
                {
                    strWhere = "MaDMA ='" + (txtMaDMA.Text) + "'";
                }
            }

            if (comboBoxNuocSanXuat.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND NuocSanXuat = " + (comboBoxNuocSanXuat.SelectedValue);
                }
                else
                {
                    strWhere = "NuocSanXuat = " + (comboBoxNuocSanXuat.SelectedValue);
                }
            }

            if (comboBoxVatLieuOngNhanh.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND VatLieuOngNhanh ='" + (comboBoxVatLieuOngNhanh.SelectedValue) + "'";
                }
                else
                {
                    strWhere = "VatLieuOngNhanh ='" + (comboBoxVatLieuOngNhanh.SelectedValue) + "'";
                }
            }

            if (comboBoxCoOng.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND CoOng = " + (comboBoxCoOng.SelectedValue);
                }
                else
                {
                    strWhere = "CoOng = " + (comboBoxCoOng.SelectedValue);
                }
            }

            if (comboBoxQuanHuyen.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND Huyen =N'" + (comboBoxQuanHuyen.SelectedValue) + "'";
                }
                else
                {
                    strWhere = "Huyen =N'" + (comboBoxQuanHuyen.SelectedValue) + "'";
                }
            }

            if (comboBoxTuyenDuong.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND TenDuong =N'" + (comboBoxTuyenDuong.SelectedValue) + "'";
                }
                else
                {
                    strWhere = "TenDuong =N'" + (comboBoxTuyenDuong.SelectedValue) + "'";
                }
            }

            if (comboBoxPhuongXa.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND Phuong =N'" + (comboBoxPhuongXa.SelectedValue) + "'";
                }
                else
                {
                    strWhere = "Phuong =N'" + (comboBoxPhuongXa.SelectedValue) + "'";
                }
            }

            // ITable pTable = pFWS.OpenTable("gis_water.DBO.fcDongHoKhachHang") as ITable;

            IQueryDef queryDef = pFWS.CreateQueryDef();

            queryDef.Tables = sTableName;

            queryDef.SubFields = "CoOng,NuocSanXuat,VatLieuOngNhanh,TenDuong,Phuong,Huyen,NgayLapDat,MaDMA,ChieuDai";

            queryDef.WhereClause = strWhere;
            DIWAGIS.Class.WorkingWithTable pC = new DIWAGIS.Class.WorkingWithTable();
            ITable pTable = pC.CreateQueryTable(DIWAGIS.Modules.generalVariable.ICurrentWorkspace, queryDef, sTableName);
            ICursor cur = pTable.Search(null, false);
            IRow curRow = null;
            double sChieuDai = 0;
            //int fldIndex = pTable.Fields.FindField("ChieuDai");
            sNResuilt = pTable.RowCount(queryDef as IQueryFilter);
            int stateNameIndex = cur.Fields.FindField("ChieuDai");
            while ((curRow = cur.NextRow()) != null)
            {
                if (!string.IsNullOrEmpty(curRow.get_Value(stateNameIndex).ToString()))
                {
                    sChieuDai = sChieuDai + Convert.ToDouble(curRow.get_Value(stateNameIndex).ToString());

                }

            }


            //sNResuilt = pTable.RowCount(queryDef as IQueryFilter);

            dataGridView1.Rows.Add(comboBoxCoOng.Text, comboBoxNuocSanXuat.Text, comboBoxVatLieuOngNhanh.Text, comboBoxTuyenDuong.Text, comboBoxPhuongXa.Text, comboBoxQuanHuyen.Text, "", sChieuDai.ToString());
           

        }
    }
}
